<template>
  <div class="container">
    <el-form ref="queryForm" :model="queryParams" size="small" :inline="true">
      <el-form-item label="接口路径" prop="url" style="margin-left: 10px;">
        <el-input
          v-model="queryParams.url"
          placeholder="请输入接口路径"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="权限码" prop="permissionCode">
        <el-input
          v-model="queryParams.permissionCode"
          placeholder="请输入权限码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="权限名" prop="permissionName">
        <el-input
          v-model="queryParams.permissionName"
          placeholder="请输入部门名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item style="float: right;margin: 20px;">
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        <el-button type="danger" @click="handleAdd">添加</el-button>
      </el-form-item>
    </el-form>

    <el-table
      v-if="refreshTable"
      v-loading="loading"
      :data="permissionPaths"
    >
      <el-table-column prop="id" label="编号" width="100" />
      <el-table-column prop="urlId" label="接口编号" width="100" />
      <el-table-column prop="url" label="接口路径" width="200" />
      <el-table-column prop="description" label="接口简介" width="200" />
      <el-table-column prop="permissionId" label="权限编号" width="50" />
      <el-table-column prop="permissionName" label="权限名" width="80" />
      <el-table-column prop="permissionCode" label="权限码" width="80" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            v-if="scope.row.parentId != 0"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 添加或修改部门对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="路径地址" prop="url">
              <el-input v-model="form.url" placeholder="请输入路径地址" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="权限编码" prop="permissionCode">
              <el-select v-model="form.permissionCode" placeholder="请选择权限码">
                <el-option
                  v-for="item in permissionCodes"
                  :key="item.permissionCode"
                  :label="item.permissionName"
                  :value="item.permissionCode"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="接口简介" prop="description">
              <el-input v-model="form.description" placeholder="请输入接口简介" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { getPermissionPath,
  updatePermissionPath,
  addPermissionPath,
  getPermissionPathById,
  getPermissionCode } from '@/api/permission-path'
export default {
  name: 'Dept',
  data() {
    return {
      queryParams: {
        url: '',
        permissionCode: '',
        permissionName: ''
      },
      open: false,
      title: '',
      permissionCodes: [],
      permissionPaths: [],
      // 重新渲染表格状态
      refreshTable: true,
      form: {},
      rules: {
        url: [
          { required: true, message: '接口路径不能为空', trigger: 'blur' }
        ],
        permissionCode: [
          { required: true, message: '权限码不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.handleQuery()
  },
  methods: {
    handleQuery() {
      this.loading = true
      getPermissionPath(this.queryParams).then(response => {
        this.permissionPaths = response.data
        this.loading = false
      })
    },
    initPermissionCode() {
      getPermissionCode().then(response => {
        this.permissionCodes = response.data
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.resetForm()
    },
    // 表单重置
    resetForm() {
      this.form = {
        url: undefined,
        permissionCode: undefined,
        permissionName: undefined
      }
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams = {
        url: undefined,
        permissionCode: undefined,
        permissionName: undefined
      }
      this.handleQuery()
    },
    /** 新增按钮操作 */
    handleAdd(row) {
      this.resetForm()
      if (row !== undefined) {
        this.form.parentId = row.deptId
      }
      this.open = true
      this.title = '添加接口'
      this.initPermissionCode()
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      getPermissionPathById(row.id).then(response => {
        this.form = response.data
        this.initPermissionCode()
        this.open = true
        this.title = '修改接口'
      })
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs['form'].validate(valid => {
        if (valid) {
        // eslint-disable-next-line eqeqeq
          if (this.form.id != undefined) {
            updatePermissionPath(this.form).then(response => {
              this.$message({
                message: response.data,
                type: 'success'
              })
              this.open = false
              this.handleQuery()
            })
          } else {
            addPermissionPath(this.form).then(response => {
              this.$message({
                message: response.data,
                type: 'success'
              })
              this.open = false
              this.handleQuery()
            })
          }
        }
      })
    }
  }
}

</script>
<style lang="scss">

</style>
